﻿using AutoMapper;
using MediatR;
using Notification.API.Application.Controller.Notification;
using Notification.Errorcode;
using Notification.Errorcode.ErrorCode;
using Notification.Infrasturcture.Interfaces;
using Notification.Infrasturcture.Repositorys;

namespace Notification.API.Application.CommandHandler.Notifications
{
    public class DeleteNotificationCommandHandler : IRequestHandler<DelNotificationCommand,ApiResult>
    {
        private readonly IDeleteProductDeyailsRepository deleteProductDeyailsRepository;

        private readonly IMapper mapper;

        private readonly ILogger<DeleteNotificationCommandHandler> logger;

        public DeleteNotificationCommandHandler(IDeleteProductDeyailsRepository deleteProductDeyailsRepository, IMapper mapper, ILogger<DeleteNotificationCommandHandler> logger)
        {
            this.deleteProductDeyailsRepository = deleteProductDeyailsRepository;
            this.mapper = mapper;
            this.logger = logger;
        }

        public async Task<ApiResult> Handle(DelNotificationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // id查询生产工单
                var deleteNotification = await deleteProductDeyailsRepository.GetAsync(c => c.Productid == request.Productid);
                if (deleteNotification == null)
                {
                    return ApiResult.Fail("删除失败，当前数据不存在", StatusCodeEnum.BadRequest);
                }
                // 删除生产工单
                var result = await deleteProductDeyailsRepository.DeleteAsync(deleteNotification);
                return ApiResult.Success(StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "生产工单删除失败");
                throw;
            }
        }
    }
}
